package com.blue.base.oauth.gateway.config.filters;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 审计日志，通过过滤器实现拦截器日志功能
 *
 * @author liulei
 * @version 1.0
 */
@Slf4j
@Component
public class AuditLogFilter extends AbstractWebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("1.审计日志过滤器......");
        ServerHttpResponseDecorator decoratedResponse = this.getResponseData(exchange, originalResponse -> {
            log.info("日志审计返回结果:{}", originalResponse);
            // TODO 记录核心日志
            return originalResponse;
        });
        return chain.filter(exchange.mutate().response(decoratedResponse).build());
    }

    @Override
    public int getOrder() {
        return -70;
    }
}
